// ActionScript file
import mx.events.SliderEvent;
import mx.collections.ArrayCollection;
import model.*;
import mx.controls.List;
import mx.controls.Alert;
import components.*;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.ID3Info;


		
//skin embeded resource class declare	
[Embed("single_assets/play_h.jpg")]
public var _skin_play_over:Class;
[Embed("single_assets/play_d.jpg")]
public var _skin_play_down:Class;
[Embed("single_assets/play.jpg")]
public var _skin_play_up:Class;
[Embed("single_assets/play_n.jpg")]
public var _skin_play_disable:Class;
			
[Embed("single_assets/stop_h.jpg")]
public var _skin_pause_over:Class;
[Embed("single_assets/stop_d.jpg")]
public var _skin_pause_down:Class;
[Embed("single_assets/stop.jpg")]
public var _skin_pause_up:Class;
[Embed("single_assets/stop_n.jpg")]
public var _skin_pause_disable:Class;



private var player:PlayerModel = null;


private function _initEnviorment():void{
		var songUrl:String = Application.application.parameters.songUrl;
		
		if(songUrl == null || songUrl == ''){
			this.btn_play.enabled = false;
			return;
		}
		var playList:PlayList = new PlayList();
		var song:Song = new Song();
		song.url = songUrl;
		playList.append(song);
		player.appendPlayList(playList);
		
}
private function _initUIComponent():void{
		this.player = new PlayerModel();
		this.player.addEventListener(PlayerEvent.MediaOpen,_playerEventHandler);
		this.player.addEventListener(PlayerEvent.Stop,_playerEventHandler);
		this.player.addEventListener(PlayerEvent.Pause,_playerEventHandler);
		this.player.addEventListener(PlayerEvent.VolumeChange,_playerEventHandler);
		this.player.addEventListener(PlayerEvent.MediaEnd,_playerEventHandler);
		this.player.addEventListener(PlayerEvent.PlayListsChange,_playerEventHandler);
		this.btn_play.addEventListener(MouseEvent.CLICK,this._play);
		
		this.img_mask_youyoulink.addEventListener(MouseEvent.CLICK,function(evt:MouseEvent):void{
			navigateToURL(new URLRequest('http://www.youyou.com'));
		});
		
		this.lbl_songName.text = Application.application.parameters.songTitle;
		var songID:String = Application.application.parameters.songID;
		
		this.img_mask_songlink.addEventListener(MouseEvent.CLICK,function(evt:MouseEvent):void{
			navigateToURL(new URLRequest('http://www.youyou.com/song/'+songID+'.html'));
		});
		
} 

private function init():void{
	this._initUIComponent();
	this._initEnviorment();
}
private function _playerEventHandler(evt:PlayerEvent):void{
				
		switch(evt.type){
			case PlayerEvent.MediaOpen:	
			break;
			case PlayerEvent.Stop:
			break;
			case PlayerEvent.MediaEnd:
			break;
		}			
		_updateCtrl(evt.playerState);
}
//update UI control status code segement
private function _updateCtrl(state:PlayerState):void{
		this.btn_play.enabled = (player.playLists.length>0)?true:false;

		if(state.engineState == PlayerState.PLAYING){
			this.btn_play.removeEventListener(MouseEvent.CLICK,this._play);
			this.btn_play.addEventListener(MouseEvent.CLICK,this._pause);
			this.btn_play.setStyle('upSkin',this._skin_pause_up);
			this.btn_play.setStyle('downSkin',this._skin_pause_down);
			this.btn_play.setStyle('overSkin',this._skin_pause_over);
			this.btn_play.setStyle('disabledSkin',this._skin_pause_disable);
		}else{
			this.btn_play.removeEventListener(MouseEvent.CLICK,this._pause);
			this.btn_play.addEventListener(MouseEvent.CLICK,this._play);
			this.btn_play.setStyle('upSkin',this._skin_play_up);
			this.btn_play.setStyle('downSkin',this._skin_play_down);
			this.btn_play.setStyle('overSkin',this._skin_play_over);
			this.btn_play.setStyle('disabledSkin',this._skin_play_disable);
		}
		
}

private function _play(evt:Event):void{
	player.play();
}
private function _pause(evt:Event):void{
	player.pause();
}
